package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.AdverService;
import com.yuwubao.uav.tool.service.UserService;
import com.yuwubao.uav.tool.util.ArticleSearchService;
import com.yuwubao.uav.tool.util.MD5Util;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 * 广告管理
 */
@RestController
@RequestMapping("/bk")
public class AdvertiseController {

    private static final Logger logger = LogManager.getLogger(AdvertiseController.class);

    @Resource
    private AdverService adverService;

    @LoginRequired
    @PostMapping("/disableOrEnableAdver")
    @ApiOperation(value = "更新广告状态 ，即审批")
    public JsonResult disableOrEnableAdver(
            @ApiParam(value = "广告ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("state", state);

            adverService.disableOrEnableAdver(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addAdver")
    @ApiOperation(value = "新增广告")
    public JsonResult addAdver(
            @ApiParam(value = "广告图片url", required = true) @RequestParam("img") String img,
            @ApiParam(value = "广告位置 1:上  2：中 3：下 4:轮播图", required = true) @RequestParam("position") String position,
            @ApiParam(value = "广告内容") @RequestParam(value = "content", required = false) String content,
            @ApiParam(value = "广告类型 1:图片广告  2：专题广告", required = true) @RequestParam("type") String type,
            @RequestParam("theme_ad_id") String theme_ad_id
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("img", img);
            pageData.put("position", position);
            pageData.put("content", content);
            pageData.put("type", type);
            pageData.put("theme_ad_id", theme_ad_id);
            adverService.addAdver(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/updateAdver")
    @ApiOperation(value = "更新广告")
    public JsonResult updateAdver(
            @ApiParam(value = "广告ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "广告图片url", required = true) @RequestParam("img") String img,
            @ApiParam(value = "广告位置 1:上  2：中 3：下 4:轮播图", required = true) @RequestParam("position") String position,
            @ApiParam(value = "广告内容") @RequestParam(value = "content", required = false) String content,
            @ApiParam(value = "广告类型 1:图片广告  2：专题广告", required = true) @RequestParam("type") String type,
            @RequestParam("theme_ad_id") String theme_ad_id
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("img", img);
            pageData.put("position", position);
            pageData.put("content", content);
            pageData.put("type", type);
            pageData.put("theme_ad_id", theme_ad_id);

            adverService.updateAdver(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/deleteAdver")
    @ApiOperation(value = "删除广告")
    public JsonResult deleteAdver(
            @ApiParam(value = "广告ID,多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            adverService.deleteAdver(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listAdver")
    @ApiOperation(value = "获取广告列表")
    public JsonResult listAdver(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 2) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "广告类型 1:图片广告  2：专题广告") @RequestParam(value = "type", required = false) String type,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey", required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("type", type);
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(adverService.listAdver(pageData));
            result.setResultNum(adverService.listAdverCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

}
